﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Windows.Forms;

namespace DotNet.WinForm.User
{
    using DotNet.Model;
    using DotNet.Service;
    using DotNet.Utilities;
    using DotNet.WinForm.Utilities;

    /// <summary>
    /// FrmUserInfo.cs
    /// 操作员属性-操作员属性窗体
    ///
    /// 修改记录
    ///
    ///     2008.04.05 版本：1.1 DotNet 整理调试程序。
    ///     2007.07.02 版本：1.0 DotNet  操作员属性页面编写。
    ///
    /// 版本：1.1
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2008.04.05</date>
    /// </author>
    /// </summary>
    public partial class FrmUserEdit : BaseForm
    {
        /// <summary>
        /// 用户实体
        /// </summary>
        private BaseUserEntity userEntity = null;

        public FrmUserEdit()
        {
            InitializeComponent();
        }

        #region public override void SetHelp() 设置帮助信息

        /// <summary>
        /// 设置帮助信息
        /// </summary>
        public override void SetHelp()
        {
            this.HelpButton = true;
            this.helpProvider.HelpNamespace = BaseSystemInfo.HelpNamespace;
            this.helpProvider.SetHelpString(this.lblUserName, "在此处键入用户名。(建议用英文)");
            this.helpProvider.SetHelpString(this.lblFullName, "在此处键入全称。(建议用中文)");
            this.helpProvider.SetHelpString(this.lblCode, "在此处键入用户编号。(建议用数字)");
            this.helpProvider.SetHelpString(this.lblDefaultRole, "在此处选择类型。");
        }

        #endregion public override void SetHelp() 设置帮助信息

        #region public FrmUserEdit(string id) : this() 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id">主键</param>
        public FrmUserEdit(string id)
            : this()
        {
            this.EntityId = id;
        }

        #endregion public FrmUserEdit(string id) : this() 构造函数

        #region private void GetRoles() 获取角色列表

        /// <summary>
        /// 获取角色列表
        /// </summary>
        private void GetRoles()
        {
            // 绑定类型数据
            DataTable dataTable = ServiceManager.Instance.UserService.GetRoleDT(UserInfo);
            BaseBusinessLogic.SetFilter(dataTable, BaseRoleTable.FieldDeletionStateCode, "0");
            BaseBusinessLogic.SetFilter(dataTable, BaseRoleTable.FieldEnabled, "1");
            DataRow dataRow = dataTable.NewRow();
            dataTable.Rows.InsertAt(dataRow, 0);
            this.cmbDefaultRole.DisplayMember = BaseRoleTable.FieldRealName;
            this.cmbDefaultRole.ValueMember = BaseRoleTable.FieldId;
            this.cmbDefaultRole.DataViewSource = dataTable.DefaultView;
        }

        #endregion private void GetRoles() 获取角色列表

        #region public override void ShowEntity() 显示数据

        /// <summary>
        /// 显示数据
        /// </summary>
        public override void ShowEntity()
        {
            // 在页面上显示
            this.txtUserName.Text = userEntity.UserName;
            this.txtRealName.Text = userEntity.RealName;
            this.txtCode.Text = userEntity.Code;
            if (userEntity.RoleId != null)
            {
                this.cmbDefaultRole.SelectedValue = userEntity.RoleId.ToString();
            }

            this.chbEnabled.Checked = userEntity.Enabled == 1;

            // 判断数据是否被其他人删除
            if (userEntity.Id == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(AppMessage.MSG0005, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        #endregion public override void ShowEntity() 显示数据

        #region public override void FormOnLoad() 加载窗体

        /// <summary>
        /// 加载窗体
        /// </summary>
        public override void FormOnLoad()
        {
            // 加载用户
            this.userEntity = ServiceManager.Instance.UserService.GetEntity(UserInfo, this.EntityId);

            // 绑定下拉框
            this.GetRoles();

            // 显示内容
            this.ShowEntity();

            // 焦点
            this.txtUserName.Focus();
        }

        #endregion public override void FormOnLoad() 加载窗体

        #region public override bool CheckInput() 检查输入的有效性

        /// <summary>
        /// 检查输入的有效性
        /// </summary>
        /// <returns>有效</returns>
        public override bool CheckInput()
        {
            bool returnValue = true;
            this.txtUserName.Text = this.txtUserName.Text.TrimEnd();
            if (this.txtUserName.Text.Trim().Length == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0007, AppMessage.MSG9978), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtUserName.Focus();
                return false;
            }
            this.txtRealName.Text = this.txtRealName.Text.TrimEnd();
            if (this.txtRealName.Text.Trim().Length == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0007, AppMessage.MSG9978), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtRealName.Focus();
                return false;
            }
            return returnValue;
        }

        #endregion public override bool CheckInput() 检查输入的有效性

        private BaseUserEntity GetEntity()
        {
            // 读取类属性
            // 更新到数据库
            // 转换数据
            userEntity.RealName = this.txtRealName.Text;
            userEntity.UserName = this.txtUserName.Text;
            userEntity.Code = this.txtCode.Text;
            int roleId = 0;
            if (this.cmbDefaultRole.SelectedValue != null && this.cmbDefaultRole.SelectedValue.ToString().Length > 0)
            {
                int.TryParse(this.cmbDefaultRole.SelectedValue.ToString(), out roleId);
                userEntity.RoleId = roleId;
            }
            else
            {
                userEntity.RoleId = null;
            }
            userEntity.Enabled = this.chbEnabled.Checked ? 1 : 0;
            return this.userEntity;
        }

        #region public override bool SaveEntity() 保存

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns>成功</returns>
        public override bool SaveEntity()
        {
            bool returnValue = false;

            // 设置鼠标繁忙状态
            this.Cursor = Cursors.WaitCursor;
            try
            {
                string statusCode = string.Empty;
                string statusMessage = string.Empty;
                this.GetEntity();
                ServiceManager.Instance.UserService.UpdateUser(UserInfo, userEntity, out statusCode, out statusMessage);

                // ServiceManager.Instance.UserService.UpdateUser(UserInfo, this.EntityId, this.txtUserName.Text, this.txtFullName.Text, this.txtCode.Text, role, string.Empty, string.Empty, string.Empty, this.chbEnabled.Checked, out statusCode, out statusMessage);
                if (statusCode == StatusCode.OKUpdate.ToString())
                {
                    if (BaseSystemInfo.ShowInformation)
                    {
                        // 添加成功，进行提示
                        DevExpress.XtraEditors.XtraMessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    this.DialogResult = DialogResult.OK;
                    return true;
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (statusCode == StatusCode.ErrorUserExist.ToString())
                    {
                        this.txtUserName.SelectAll();
                        this.txtUserName.Focus();
                    }

                    // 是否编号重复了，提高友善性
                    if (statusCode == StatusCode.ErrorCodeExist.ToString())
                    {
                        this.txtCode.SelectAll();
                        this.txtCode.Focus();
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                this.ProcessException(ex);
            }
            finally
            {
                // 鼠标正常状态
                this.Cursor = Cursors.Default;
            }
            return returnValue;
        }

        #endregion public override bool SaveEntity() 保存

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                // 设置鼠标繁忙状态
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    this.ProcessException(ex);
                }
                finally
                {
                    // 设置鼠标默认状态
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}